import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';

const workspaceFolders = vscode.workspace.workspaceFolders;

const CONFIG_FILE_PATH = path.join(workspaceFolders?.[0].uri.fsPath ?? '', '.gitsubtree');

export function getConfig() {
  if (!fs.existsSync(CONFIG_FILE_PATH)) {
    return '';
  }
  const fileContent = fs.readFileSync(CONFIG_FILE_PATH, 'utf-8');

  try {
    return JSON.parse(fileContent);
  } catch (error) {
    console.error("配置文件格式错误");
    process.exit(1);
  }
}

export function writeConfig(content: object) {
  fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(content, null, 4));
  return true;
}


